const util = require('../../../utils/util.js')
function requestSystemMsg(page, callback) {
  var userId = getApp().globalData.userId
  getApp().func.Post(false, getApp().api.user_sys_msg, { "sysFlag": 1, "toId": userId, "startPage" : page, "pageSize": 20},
  function(data) {
      typeof callback == "function" && callback(data)
  },
  function(error) {
    console.log(error)
    wx.showToast({
      title: error.data.message,
      icon: 'none',
      duration: 2000
    })
  })
};
// 获取聊天列表
function requestChatMsg(page, callback) {
  // var userId = getApp().globalData.userId
  // var userId = "2"
  getApp().func.Get(false, getApp().api.user_list_msg, {"startPage" : page, "pageSize": 20},
  function(data) {
      typeof callback == "function" && callback(data)
  },
  function(error) {
    console.log(error)
    wx.showToast({
      title: error.data.message,
      icon: 'none',
      duration: 2000
    })
  })
};

// 获取公告列表
function requestNoticeMsg(page,type, callback) {
  var userId = getApp().globalData.userId
  getApp().func.Post(false, getApp().api.user_list_notice, { "userId": userId, "startPage": page, "type": type },
    function (data) {
      typeof callback == "function" && callback(data)
    },
    function (error) {
      console.log(error)
      wx.showToast({
        title: error.data.message,
        icon: 'none',
        duration: 2000
      })
    })
};
// 获取聊天未读
function requestUnReadChat(callback) {
  var userId = getApp().globalData.userId
  getApp().func.Get(false, getApp().api.user_unread_num, { "toId": userId },
    function (data) {
      typeof callback == "function" && callback(data)
    },
    function (error) {
      console.log(error)
      wx.showToast({
        title: error.data.message,
        icon: 'none',
        duration: 2000
      })
    })
};
//读取公告
function readNotice(callback) {
  var userId = getApp().globalData.userId
  getApp().func.Post(false, getApp().api.readNotice, { "userId": userId},function(res){
    typeof callback == "function" && callback(res)
  })
};
// 获取系统未读
function requestUnReadSys(callback) {
  var userId = getApp().globalData.userId
  getApp().func.Get(false, getApp().api.user_list_sys_unread_num, {"readStatus":0, "sysFlag":1,"toId": userId },
    function (data) {
      typeof callback == "function" && callback(data)
    },
    function (error) {
      console.log(error)
      wx.showToast({
        title: error.data.message,
        icon: 'none',
        duration: 2000
      })
    })
};

/**
 * 读取系统消息
 */
function readMsg(callback) {
  var userId = getApp().globalData.userId
  getApp().func.Post(false, getApp().api.read_msg, { "toId": userId },
    function (data) {
      typeof callback == "function" && callback(data)
    },
    function (error) {
      console.log(error)
      wx.showToast({
        title: error.data.message,
        icon: 'none',
        duration: 2000
      })
    })
};


module.exports = {
  readMsg:readMsg,
  readNotice: readNotice,
  requestSystemMsg: requestSystemMsg,
  requestChatMsg: requestChatMsg,
  requestNoticeMsg: requestNoticeMsg,
  requestUnReadChat: requestUnReadChat,
  requestUnReadSys: requestUnReadSys,
  readNotice: readNotice
};